import { Advisement } from '../models';

export default function (router) {
  /**
   * 前端接口
   * 获取advisement列表
   */
  router.get('/api/front/advisement', async ctx => {
    const { page, limit } = ctx.parsePageAndLimit(ctx.request.query);
    const list = await Advisement.find({}, '-url_params')
      .skip((page - 1) * limit)
      .limit(limit)
      .sort({ priority: 1, show: -1, create_time: 1 });
    const total = await Advisement.countDocuments();
    await ctx.json(true, '获取图片推荐成功', { list, total });
  });

  /**
   * 后端接口
   * 获取advisement列表
   */
  router.get('/api/admin/advisement', async ctx => {
    const { page, limit } = ctx.parsePageAndLimit(ctx.request.query);
    const { search } = ctx.request.query;
    const queryParam = {};
    if (search) {
      const reg = new RegExp(search.toString('utf8').trim(), 'i');
      queryParam.des = reg;
    }
    const list = await Advisement.find(queryParam)
      .skip((page - 1) * limit)
      .limit(limit)
      .sort({ priority: 1, show: -1, create_time: 1 });
    const total = await Advisement.countDocuments();
    await ctx.json(true, '获取图片推荐成功', { list, total });
  });

  /**
   * 后端接口
   * 新增图片推荐
   */
  router.post('/api/admin/advisement', async ctx => {
    const { show, url, url_params, img_url, tags, des } = ctx.request.body;
    const maxPriority = await Advisement.countDocuments();
    const result = await Advisement.create({
      priority: maxPriority + 1,
      show,
      url,
      url_params,
      img_url,
      tags,
      des,
      create_time: new Date()
    });
    await ctx.json(true, '新增图片推荐成功', { result, total: maxPriority + 1 });
  });

  /**
   * 后端接口
   * 修改图片推荐
   */
  router.put('/api/admin/advisement/:id', async ctx => {
    const id = ctx.params.id;
    const result = await Advisement.updateOne({ _id: id }, { $set: ctx.request.body });
    if (result.ok === 1) {
      const newest = await Advisement.findById(id);
      await ctx.json(true, '更新图片推荐成功', newest);
    } else {
      await ctx.json(false, '更新图片推荐失败', result);
    }
  });

  /**
   * 后端接口
   * 删除图片推荐
   */
  router.delete('/api/admin/advisement/:id', async ctx => {
    const id = ctx.params.id;
    const thisData = await Advisement.findOne({ _id: id }, 'priority');
    const operation = await Advisement.deleteOne({ _id: id });
    // 更新顺序
    const update = await Advisement.updateMany({ priority: { $gt: thisData.priority } }, { $inc: { priority: -1 } });
    if (operation.ok && update.ok) {
      await ctx.json(true, '删除图片推荐成功');
    } else {
      await ctx.json(false, '删除图片推荐失败');
    }
  });

  /**
   * 后端接口
   * 交换图片推荐位置
   */
  router.post('/api/admin/advisement/exchange', async ctx => {
    const { from: fromIndex, to: toIndex, id } = ctx.request.body;
    if (!fromIndex || !toIndex) await ctx.json(false, '参数错误');
    if (fromIndex === toIndex) await ctx.json(false, '交换顺序不能相同');

    let updateCur, updateOther;
    if (fromIndex > toIndex) {
      updateOther = await Advisement.updateMany({ priority: { $lt: fromIndex, $gte: toIndex } }, { $inc: { priority: 1 } });
      updateCur = await Advisement.updateOne({ _id: id }, { $set: { priority: toIndex } });
    } else {
      updateOther = await Advisement.updateMany({ priority: { $gt: fromIndex, $lte: toIndex } }, { $inc: { priority: -1 } });
      updateCur = await Advisement.updateOne({ _id: id }, { $set: { priority: toIndex } });
    }

    if (updateCur.ok && updateOther.ok) {
      await ctx.json(true, '交换成功');
    } else {
      await ctx.json(false, '交换失败');
    }
  });
}
